package com.asset.dispoal.business.casedispose.service.command;

import com.asset.dispoal.business.bsCase.domain.db.entity.CaseStateRecord;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.business.casedispose.dto.CaseDisposePreLitigationMediationRequest;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

@Component
public class CaseDisposePreLitigationMediationCommand implements BaseCommand<CaseDisposePreLitigationMediationRequest, Result> {

    @Autowired
    private BsCaseMapper caseMapper;

    @Autowired
    private BaseIdeable<String> ideable;

    @Override
    public Result execute(CaseDisposePreLitigationMediationRequest request) {
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        CaseStateRecord caseStateRecord = new CaseStateRecord();
        caseStateRecord.setId(ideable.generateId());
        caseStateRecord.setOldState(AgreementCaseSateMenu.WAIT_UP_CASE.getLabel());
        caseStateRecord.setNewState(AgreementCaseSateMenu.PRE_LITIGATION_MEDIATION.getLabel());
        caseStateRecord.setCaseId(request.getId());
        caseStateRecord.setCreateUser(userId);
        caseMapper.insertStateRecord(caseStateRecord);
        caseMapper.updateCaseState(request.getId(),
                AgreementCaseSateMenu.PRE_LITIGATION_MEDIATION.getLabel(),
                AgreementCaseSateMenu.PRE_LITIGATION_MEDIATION.getState(),
                userId);
        return Result.ok();
    }
}
